package org.mzy.order.service.impl;

import org.mzy.basic.config.BussinessException;
import org.mzy.basic.service.impl.BaseServiceImpl;
import org.mzy.order.domain.OrderPetAcquisition;
import org.mzy.order.dto.OrderConfirmDto;
import org.mzy.order.mapper.OrderPetAcquisitionMapper;
import org.mzy.order.service.IOrderPetAcquisitionService;
import org.mzy.pet.domain.Pet;
import org.mzy.pet.domain.SearchMasterMsg;
import org.mzy.pet.service.IPetService;
import org.mzy.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzy
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IPetService petService;

    /*确认订单*/
    @Override
    public void confirm(OrderConfirmDto orderConfirmDto) {
        // 通过订单的id获取订单信息
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderConfirmDto.getId());
        // 参数校验
        if (orderPetAcquisition == null) {
            throw new BussinessException("订单不存在！");
        }
        // 通过订单信息的寻主信息id，获取寻主信息数据
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        // 参数校验
        if (searchMasterMsg == null) {
            throw new BussinessException("寻主消息不存在！");
        }
        // 将寻主信息的状态置为3
        searchMasterMsg.setState(3);
        searchMasterMsgService.update(searchMasterMsg);
        // 添加宠物
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(orderConfirmDto.getMoney().toString());
        pet.setTypeId(1L);
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petService.add(pet);
        // 修改订单
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(orderConfirmDto.getMoney());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }

    /*取消订单*/
    @Override
    public void cancel(Long orderId) {
        // 根据id找到订单信息
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderId);
        // 参数校验
        if (orderPetAcquisition == null) {
            throw new BussinessException("无效的订单");
        }
        // 修改订单状态为取消
        orderPetAcquisition.setState(-1);
        // 修改保存
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 根据订单的寻主信息id，找到这个信息数据
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        // 参数校验
        if (searchMasterMsg == null) {
            throw new BussinessException("寻主消息不存在");
        }
        // 将寻主信息状态赋值为1
        searchMasterMsg.setState(1);
        // 将寻主信息的店铺id赋值为null
        searchMasterMsg.setShopId(null);
        // 修改保存
        searchMasterMsgService.update(searchMasterMsg);
    }

    @Override
    public OrderPetAcquisition findByOrderSn(Long message) {
        return orderPetAcquisitionMapper.findByOrderSn(message);
    }
}